//
//  TLProjectViewController.m
//  Coding_iOS
//
//  Created by taolei-mac on 2017/11/22.
//  Copyright © 2017年 com.lianchuan. All rights reserved.
//

#import "TLProjectViewController.h"
#import "TLNewProjectViewController.h"
#import "TLNavigationViewController.h"
#import "TLChooseProjectViewController.h"
#import "TLSendTweetViewController.h"
#import "TLAddFriendsViewController.h"
#import "TLProjectSquareViewController.h"
#import "TLSearchProjectViewController.h"
#import "TLScanCodeViewController.h"
#import "TLProjectCell.h"
#import "TLPopMenu.h"
#import "TLFliterMenu.h"
#import "TLSearchBarView.h"
#import "TLMenuItem.h"
#import "TLFilterMenuGroup.h"
#import "TLFilterMenuItem.h"
#import "TLHttpTool.h"
#import "TLProjectInfo.h"

@interface TLProjectViewController ()<TLPopMenuDelegate, TLFliterMenuDelegate, UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) TLPopMenu *popMenu;

@property (nonatomic, strong) TLFliterMenu *filterMenu;

@property (nonatomic, strong) NSArray *menuItems;

@property (nonatomic, strong) NSArray *filterMenus;

@property (nonatomic, weak) UIButton *titleButton;

@property (nonatomic, strong) NSArray *projects;

@property (nonatomic, weak) UITableView *projectListView;

@end

@implementation TLProjectViewController

- (NSArray *)filterMenus
{
    if(!_filterMenus){
        TLFilterMenuGroup *group0 = [TLFilterMenuGroup group];
        TLFilterMenuItem *fliterItem0 = [TLFilterMenuItem itemWithTitle:@"全部项目" projectCount:0];
        TLFilterMenuItem *fliterItem1 = [TLFilterMenuItem itemWithTitle:@"我创建的" projectCount:0];
        TLFilterMenuItem *fliterItem2 = [TLFilterMenuItem itemWithTitle:@"我参与的" projectCount:0];
        group0.items = @[fliterItem0, fliterItem1, fliterItem2];
        
        TLFilterMenuGroup *group1 = [TLFilterMenuGroup group];
        TLFilterMenuItem *fliterItem3 = [TLFilterMenuItem itemWithTitle:@"我关注的" projectCount:0];
        TLFilterMenuItem *fliterItem4 = [TLFilterMenuItem itemWithTitle:@"我收藏的" projectCount:0];
        group1.items = @[fliterItem3, fliterItem4];
        
        _filterMenus = @[group0, group1];
    }
    return _filterMenus;
}

- (NSArray *)menuItems
{
    if(!_menuItems){
        
        _menuItems = @[[TLMenuItem itemWithTitle:@"项目" iconName:@"pop_Project" index:0],
                       [TLMenuItem itemWithTitle:@"任务" iconName:@"pop_Task" index:1],
                       [TLMenuItem itemWithTitle:@"冒泡" iconName:@"pop_Tweet" index:2],
                       [TLMenuItem itemWithTitle:@"添加好友" iconName:@"pop_User" index:3],
                       [TLMenuItem itemWithTitle:@"私信" iconName:@"pop_Message" index:4],
                       [TLMenuItem itemWithTitle:@"两步验证" iconName:@"pop_2FA" index:5]
                       ];
    }
    return _menuItems;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self configSubViews];
    
   [self getProjects];
}

- (void)getProjects
{
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:3];
    params[@"page"] = @1;
    params[@"pageSize"] = @20;
    params[@"sort"] = @"hot";
    params[@"type"] = @"all";
    
    @weakify(self)
    [TLHttpTool get:@"https://coding.net/api/projects" params:params success:^(id responseObj) {
        
        @strongify(self)
        TLLog(@"%@", responseObj);
        NSDictionary *projects = [[responseObj objectForKey:@"data"] objectForKey:@"list"];
        self.projects = [TLProjectInfo mj_objectArrayWithKeyValuesArray:projects];
        [self.projectListView reloadData];
    } failure:^(NSError *error) {
        
        
    }];
}


/**
 *  添加子控件
 */
- (void)configSubViews
{
    self.view.backgroundColor = TLColor(247, 247, 247, 1.0);
    self.customerNavigationItem.rightBarButtonItem = [UIBarButtonItem itemWithImageName:@"addBtn_Nav" highImageName:@"addBtn_Nav" target:self action:@selector(addItemClicked:)];
    
    // 自定义titleView
    UIButton *titleButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [titleButton setImage:[UIImage originImageWithName:@"btn_fliter_down"] forState:UIControlStateNormal];
    [titleButton setTitle:@"全部项目" forState:UIControlStateNormal];
    titleButton.titleLabel.font = [UIFont systemFontOfSize:18];
    [titleButton setTitleColor:[UIColor colorWithHexString:@"0x323A45"] forState:UIControlStateNormal];
    CGFloat titleWidth = [titleButton.currentTitle getWidthWithFont:titleButton.titleLabel.font constrainedToSize:CGSizeMake(TLScreenW, 30)];
    CGFloat imageWidth = 12;
    titleButton.size = CGSizeMake(titleWidth + imageWidth, 30);
    [titleButton setImageEdgeInsets:UIEdgeInsetsMake(0, titleWidth, 0, -titleWidth)];
    [titleButton setTitleEdgeInsets:UIEdgeInsetsMake(0, -imageWidth, 0, imageWidth)];
    [titleButton addTarget:self action:@selector(titleButtonClick) forControlEvents:UIControlEventTouchUpInside];
    self.customerNavigationItem.titleView = titleButton;
    self.titleButton = titleButton;
    
    // 添加一个展示项目的列表
    UITableView *projectListView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    projectListView.delegate = self;
    projectListView.dataSource = self;
    projectListView.backgroundColor = TLColor(239, 241, 244, 1.0);
    projectListView.contentInset = UIEdgeInsetsMake(TLNavigationBarH, 0, 0, 0);
    projectListView.rowHeight = 110.0;
    if(iOS11){
        
        projectListView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    projectListView.tableFooterView = [[UIView alloc] init];
    self.projectListView = projectListView;
    
    // 添加一个头部视图
    TLSearchBarView *searchBarView = [[TLSearchBarView alloc] init];
    searchBarView.height = 45;
    projectListView.tableHeaderView = searchBarView;
    [self.view addSubview:projectListView];
    searchBarView.searchBlock = ^(){
      
        [self goToSearchViewController];
    };
    
    searchBarView.scanBlock = ^(){
        
        [self goToScanViewController];
    };
}

/**
 *  跳转到项目搜索界面
 */
- (void)goToSearchViewController
{
    TLSearchProjectViewController *searchProjectVc = [[TLSearchProjectViewController alloc] init];
    TLNavigationViewController *nav = [[TLNavigationViewController alloc] initWithRootViewController:searchProjectVc];
    [self.navigationController presentViewController:nav animated:NO completion:nil];
}

- (void)goToScanViewController
{
    TLScanCodeViewController *scanCodeVc = [[TLScanCodeViewController alloc] init];
    [self.navigationController pushViewController:scanCodeVc animated:YES];
}

/**
 * 监听加号按钮点击
 */
- (void)addItemClicked:(UIButton *)sender
{
    if(!self.popMenu){
        
        self.popMenu = [[TLPopMenu alloc] initWithFrame:CGRectMake(0, TLNavigationBarH, TLScreenW, TLScreenH - TLNavigationBarH) items:self.menuItems];
        self.popMenu.delegate = self;
        [self.popMenu show];
        
        @weakify(self)
        self.popMenu.selectedItemBlock = ^(TLMenuItem *selectedItem){
            
            @strongify(self)
            switch (selectedItem.index) {
                case 0:
                    
                    [self goToNewProjectVc];
                    break;
                case 1:
                    
                    [self goToNewTaskVc];
                    break;
                case 2:
                    
                    [self goToSendTweetVC];
                    break;
                case 3:
                    
                    [self goToAddFriendVc];
                    break;
                    
                case 4:
                    
                    break;
                case 5:
                    
                    break;
                default:
                    break;
            }
        };
    }else{
        
        [self.popMenu dismiss];
    }
}

- (void)titleButtonClick
{
    if(!_filterMenu){
        
        self.filterMenu = [[TLFliterMenu alloc] initWithFrame:CGRectMake(0, TLNavigationBarH, TLScreenW, TLScreenH - TLNavigationBarH) items:self.filterMenus];
        self.filterMenu.delegate = self;
        [self.filterMenu show];
        
        @weakify(self)
        self.filterMenu.clickBlock = ^(TLFilterMenuItem *menuItem){
            
            @strongify(self)
            if(menuItem){
                
                [self.titleButton setTitle:menuItem.title forState:UIControlStateNormal];
            }else{
                
                TLProjectSquareViewController *projectSquareVc = [[TLProjectSquareViewController alloc] init];
                [self.navigationController pushViewController:projectSquareVc animated:YES];
            }
        };
    }else{
        
        [self.filterMenu close];
    }
}

- (void)goToNewProjectVc
{
    TLNewProjectViewController *newProjectVc = [[TLNewProjectViewController alloc] init];
    [self.navigationController pushViewController:newProjectVc animated:YES];
}

- (void)goToNewTaskVc
{
    TLChooseProjectViewController *newTaskVc = [[TLChooseProjectViewController alloc] init];
    [self.navigationController pushViewController:newTaskVc animated:YES];
}

- (void)goToSendTweetVC
{
    TLSendTweetViewController *sendTweetVc = [[TLSendTweetViewController alloc] init];
    TLNavigationViewController *sendTweetNav = [[TLNavigationViewController alloc] initWithRootViewController:sendTweetVc];
    [self presentViewController:sendTweetNav animated:YES completion:nil];
}

- (void)goToAddFriendVc
{
    TLAddFriendsViewController *addFriendVc = [[TLAddFriendsViewController alloc] init];
    [self.navigationController pushViewController:addFriendVc animated:YES];
}

#pragma mark -- TLPopMenuDelegate
/**
 * TLPopMenu消失后通知self.popMenu置空
 */
- (void)popMenuDismiss
{
    self.popMenu = nil;
}

#pragma mark -- TLFliterMenuDelegate
- (void)filterMenuDismiss
{
    self.filterMenu = nil;
}

#pragma mark -- UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.projects.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TLProjectCell *cell = [TLProjectCell createProjectCellWithTableView:tableView];
    TLProjectInfo *projectInfo = self.projects[indexPath.row];
    cell.projectInfo = projectInfo;
    return cell;
}

@end
